<template>
    <div>
        <el-dialog v-model="dialogVisible" width="80%" align-center :close-on-click-modal="false" :show-close="false"
            :before-close="handleClose" destroy-on-close draggable>
            <template #header="{ close }">
                <div class="dialog-header flex justify-between items-center">
                    <div>专家评审</div>
                    <el-icon class="el-icon--left cursor-pointer" @click="close">
                        <CircleCloseFilled color="#fff" />
                    </el-icon>
                </div>
            </template>
            <el-form :model="ruleForm" :rules="rules" label-width="130px" ref="ruleFormRef">
                <!-- 评分详情 -->
                <div class="bg-white mt-[10px]">
                    <BaseHead>评分详情</BaseHead>
                    <div class="p-[10px]">
                        <el-descriptions title="" :column="2" border label-width="140">
                            <el-descriptions-item :span="1" label-class-name="gdlable" label="成果名称">{{
            ruleForm.productName
        }}</el-descriptions-item>
                        </el-descriptions>
                        <el-table :data="ruleForm.reviewStandardVOS" max-height="480" stripe style="width: 100%;">
                            <el-table-column prop="name" label="一级标准" />
                            <el-table-column prop="name" label="二级标准">
                                <template #default="scope">
                                    <div v-for="item in scope.row.lvl2Names" :key="item.value" class="mt-[10px]">{{
            item.value }}</div>
                                </template>
                            </el-table-column>
                            <el-table-column prop="address" label="满分">
                                <template #default="scope">
                                    <div v-for="item in scope.row.lvl2Marks" :key="item.value" class="mt-[10px]">{{
            item.value }}</div>
                                </template>
                            </el-table-column>
                            <el-table-column prop="address" label="评审得分"> <template #default="scope">
                                    <!-- <div v-for="item in scope.row.lvl2Marks" :key="item.value" class="mt-[10px]">

                                    </div> -->
                                    <el-form-item label-width="0"
                                        :prop="'reviewStandardVOS.' + scope.$index + '.lvl2Marks.' + index + '.score'"
                                        :rules="{ required: true, message: '必填', trigger: 'blur' }"
                                        v-for=" (item, index) in scope.row.lvl2Marks" :key="index">
                                        <el-input v-model="item.score" type="number" @blur="e => {
            blurInput(e, item.score, scope.row, index, scope.$index);
        }" class="mt-[10px]" />
                                    </el-form-item>
                                </template>
                            </el-table-column>
                        </el-table>
                    </div>
                </div>
            </el-form>
            <template #footer>
                <div class="dialog-footer">
                    <div class="p-4">
                        <el-form :model="reviewForm">
                            <el-form-item label="评审意见">
                                <el-input v-model="reviewForm.reviewInfo" type="textarea" />
                            </el-form-item>
                            <el-form-item label="评审结果">
                                <el-radio-group v-model="reviewForm.result">
                                    <el-radio value="pass">建议通过</el-radio>
                                    <el-radio value="noPass">不建议通过</el-radio>
                                </el-radio-group>
                            </el-form-item>
                        </el-form>
                    </div>
                    <div class="operate-btns">
                        <el-button type="primary" @click="zcForm(ruleFormRef)">暂存</el-button>
                        <el-button type="primary" @click="submitForm(ruleFormRef)">提交</el-button>
                        <el-button type="primary" @click="dialogVisibleMet">关闭</el-button>
                    </div>
                </div>
            </template>
        </el-dialog>
    </div>
</template>
<script setup>
import BaseHead from "@/views/project/components/BaseHead.vue";
import { reviewByExpert } from "@/api/achievement/cghj"
import { ElMessage, ElMessageBox } from "element-plus";
const dialogVisible = defineModel();
const ruleForm = reactive({

})
const reviewForm = reactive({
    reviewInfo: '',
    result: 'pass',
})
const ruleFormRef = ref()
const submitForm = async (formEl) => {
    if (!formEl) return
    await formEl.validate((valid, fields) => {
        if (valid) {
            // let totleObjectNum = 0
            // ruleForm.reviewStandardVOS.forEach(element => {
            //     element.lvl2Marks.forEach(element2 => {
            //         totleObjectNum += Number(element2.score)
            //     });
            // });
            let params = {
                expertReviewId: ruleForm.allExpertReviewVOS[0].id,
                reviewObjectId: ruleForm.id,
                reviewInfo: reviewForm.reviewInfo,
                reviewResult: reviewForm.result
            }
            params.reviewResultDTOs = ruleForm.reviewStandardVOS.map(item => {
                console.log(item);
                let totleNum = 0
                item.lvl2Marks.forEach(element2 => {
                    totleNum += Number(element2.score)
                });
                return {
                    reviewStandardId: item.id,
                    reviewMark: totleNum,
                    lvl2Reviewmarks: item.lvl2Marks.map(item => item.score).join(',')
                }
            })
            params.reviewStatus = 'submit'
            reviewByExpert(params).then((res) => {
                if (res.code == 200) {
                    ElMessage({
                        message: res.msg,
                        type: 'success',
                    })
                    dialogVisible.value = false
                    ininDate()
                    emit('callBack', "")
                } else {
                    ElMessage({
                        message: res.msg,
                        type: 'error',
                    })
                }
            })
        } else {
            console.log('error submit!', fields)
        }
    })
}
const zcForm = async (formEl) => {
    if (!formEl) return
    await formEl.validate((valid, fields) => {
        if (valid) {
            let params = {
                expertReviewId: ruleForm.allExpertReviewVOS[0].id,
                reviewObjectId: ruleForm.id,
                reviewInfo: reviewForm.reviewInfo,
                reviewResult: reviewForm.result
            }
            params.reviewResultDTOs = ruleForm.reviewStandardVOS.map(item => {
                console.log(item);
                let totleNum = 0
                item.lvl2Marks.forEach(element2 => {
                    totleNum += Number(element2.score)
                });
                return {
                    reviewStandardId: item.id,
                    reviewMark: totleNum,
                    lvl2Reviewmarks: item.lvl2Marks.map(item => item.score).join(',')
                }
            })
            params.reviewStatus = 'notReview'
            reviewByExpert(params).then((res) => {
                if (res.code == 200) {
                    ElMessage({
                        message: res.msg,
                        type: 'success',
                    })
                    dialogVisible.value = false
                    ininDate()
                    emit('callBack', "")
                } else {
                    ElMessage({
                        message: res.msg,
                        type: 'error',
                    })
                }
            })
        } else {
            console.log('error submit!', fields)
        }
    })
}
const emit = defineEmits(['callBack']);
const ininDate = () => {
    Object.assign(ruleForm, {

    })
    Object.assign(reviewForm, {
        reviewInfo: '',
        result: 'pass',
    })
}
const handleClose = (done) => {
    ininDate()
    done()
}

const dialogVisibleMet = () => {
    dialogVisible.value = false;
}
const isJSON = (str) => {
    if (typeof str === 'string') {
        try {
            const obj = JSON.parse(str);
            return typeof obj === 'object' && obj !== null;
        } catch (e) {
            return false;
        }
    }
    return false;
}

const blurInput = (e, value, row, index, index1) => {
    if (Number(value) > Number(row.lvl2Marks[index].value)) {
        ElMessage({
            type: 'error',
            message: `评分不能大于满分`,
        })
        ruleForm.reviewStandardVOS[index1].lvl2Marks[index].score = row.lvl2Marks[index].value
    }
    if (Number(value) < 0) {
        ElMessage({
            type: 'error',
            message: `评分不能小于0`,
        })
        ruleForm.reviewStandardVOS[index1].lvl2Marks[index].score = 0
    }
}
const getRuleForm = (i) => {
    for (const key in i) {
        ruleForm[key] = i[key];
    }
    if (i.reviewStandardVOS && i.reviewStandardVOS.length > 0) {
        ruleForm.reviewStandardVOS = i.reviewStandardVOS.map(item => {
            if (isJSON(item.lvl2Names)) {
                return {
                    name: item.name,
                    id: item.id,
                    lvl2Names: JSON.parse(item.lvl2Names),
                    lvl2Marks: item.lvl2Marks.split(',').map(item => ({ value: item, score: null }))
                }
            } else {
                return {
                    name: item.name,
                    id: item.id,
                    lvl2Names: item.lvl2Names.split(',').map(item => ({ value: item })),
                    lvl2Marks: item.lvl2Marks.split(',').map(item => ({ value: item, score: null }))
                }
            }

        })
    }
    reviewForm.reviewInfo = i.allExpertReviewVOS[0].reviewInfo
    if (i.allExpertReviewVOS[0].reviewResult) {
        reviewForm.result = i.allExpertReviewVOS[0].reviewResult
    }

    ruleForm.allExpertReviewVOS[0].reviewResultVOs.forEach((item, index) => {
        item.lvl2Reviewmarks = item.lvl2Reviewmarks.split(',')
        item.lvl2Reviewmarks.forEach((item1, index1) => {
            ruleForm.reviewStandardVOS[index].lvl2Marks[index1].score = item1
        })
    })
    console.log(ruleForm, 'ruleForm')
}
defineExpose({
    getRuleForm
})
</script>

<style scoped lang="scss">
@import url("@/views/achievement/index.scss");
</style>
<style lang="scss">
.gdlable {
    width: 140px;
}
</style>